import { createRouter, createWebHistory } from 'vue-router'
import IndexView from '@/views/IndexView.vue'
import UserCenterView from '@/views/UserCenterView.vue'
import LoginView from '@/views/LoginView.vue'
import BanksView from '@/views/BanksView.vue'
import QuestionsView from '@/views/QuestionsView.vue'
import AdminView from '@/views/AdminView.vue'
import AIView from '@/views/AIView.vue'
import RegisterView from '@/views/RegisterView.vue'
import BankDetailView from '@/views/BankDetailView.vue'
import QuestionDetailView from '@/views/QuestionDetailView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component:  IndexView,
    },
    {
      path: '/banks',
      name: 'banks',
      component:  BanksView,
    },
    {
      path: '/bank/detail/:id',
      name: 'bank-detail',
      component:  BankDetailView,
    },
    {
      path: '/questions',
      name: 'questions',
      component:  QuestionsView,
    },
    {
      path: '/question/detail/:id',
      name: 'question-detail',
      component:  QuestionDetailView,
    },
    {
      path: '/intelligent',
      name: 'intelligent',
      component:  AIView,
    },
    {
      path: '/admin',
      name: 'admin',
      component:  AdminView,
    },
    {
      path: '/user/login',
      name: 'login',
      component:  LoginView,
    },
    {
      path: '/user/register',
      name: 'register',
      component:  RegisterView,
    },
    {
      path: '/user-center',
      name: 'user-center',
      component:  UserCenterView,
    }
  ],
})
router.beforeEach((to,from) => {
  if( to.name == 'index' || to.name == 'login' || to.name == 'register')
    return true
  const loginUser = localStorage.getItem("loginUser")
  console.log(loginUser)
  if(loginUser == null)
    return { name: 'login' }
  return true    
})

export default router
